package com.irdstudio.efp.report.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.report.service.dao.ZxbsCtrLoanContDao;
import com.irdstudio.efp.report.service.domain.ZxbsCtrLoanCont;
import com.irdstudio.efp.report.service.facade.ZxbsCtrLoanContService;
import com.irdstudio.efp.report.service.vo.ZxbsCtrLoanContVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Service Impl: 贷款合同表服务实现
 *
 * @author gcb
 * @date 2020-07-28
 */
@Service("zxbsCtrLoanContService")
public class ZxbsCtrLoanContServiceImpl implements ZxbsCtrLoanContService, FrameworkService {

    private static Logger logger = LoggerFactory.getLogger(ZxbsCtrLoanContServiceImpl.class);

    @Autowired
    private ZxbsCtrLoanContDao zxbsCtrLoanContDao;

    /**
     * 新增数据
     *
     * @param zxbsCtrLoanContVo
     * @return
     */
    @Override
    public int insert(ZxbsCtrLoanContVO zxbsCtrLoanContVo) {
        logger.debug("当前新增数据为:" + zxbsCtrLoanContVo.toString());
        int num = 0;
        try {
            ZxbsCtrLoanCont zxbsCtrLoanCont = new ZxbsCtrLoanCont();
            beanCopy(zxbsCtrLoanContVo, zxbsCtrLoanCont);
            num = zxbsCtrLoanContDao.insert(zxbsCtrLoanCont);
        } catch (Exception e) {
            logger.error("新增数据发生异常!", e);
            num = -1;
        }
        logger.debug("当前新增数据条数为:" + num);
        return num;
    }

    /**
     * 根据主键删除信息
     *
     * @param zxbsCtrLoanContVo
     * @return
     */
    @Override
    public int deleteByPk(ZxbsCtrLoanContVO zxbsCtrLoanContVo) {
        logger.debug("当前删除数据条件为:" + zxbsCtrLoanContVo);
        int num = 0;
        try {
            ZxbsCtrLoanCont zxbsCtrLoanCont = new ZxbsCtrLoanCont();
            beanCopy(zxbsCtrLoanContVo, zxbsCtrLoanCont);
            num = zxbsCtrLoanContDao.deleteByPk(zxbsCtrLoanCont);
        } catch (Exception e) {
            logger.error("删除数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + zxbsCtrLoanContVo + "删除的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键更新信息
     *
     * @param zxbsCtrLoanContVo
     * @return
     */
    @Override
    public int updateByPk(ZxbsCtrLoanContVO zxbsCtrLoanContVo) {
        logger.debug("当前修改数据为:" + zxbsCtrLoanContVo.toString());
        int num = 0;
        try {
            ZxbsCtrLoanCont zxbsCtrLoanCont = new ZxbsCtrLoanCont();
            beanCopy(zxbsCtrLoanContVo, zxbsCtrLoanCont);
            num = zxbsCtrLoanContDao.updateByPk(zxbsCtrLoanCont);
        } catch (Exception e) {
            logger.error("修改数据发生异常!", e);
            num = -1;
        }
        logger.debug("根据条件:" + zxbsCtrLoanContVo + "修改的数据条数为" + num);
        return num;
    }

    /**
     * 根据主键查询详情
     *
     * @param inZxbsCtrLoanContVo
     * @return
     */
    @Override
    public ZxbsCtrLoanContVO queryByPk(ZxbsCtrLoanContVO inZxbsCtrLoanContVo) {
        logger.debug("当前查询参数信息为:" + inZxbsCtrLoanContVo);
        try {
            ZxbsCtrLoanCont queryZxbsCtrLoanCont = new ZxbsCtrLoanCont();
            beanCopy(inZxbsCtrLoanContVo, queryZxbsCtrLoanCont);
            ZxbsCtrLoanCont queryRslZxbsCtrLoanCont = zxbsCtrLoanContDao.queryByPk(queryZxbsCtrLoanCont);
            if (Objects.nonNull(queryRslZxbsCtrLoanCont)) {
                return beanCopy(queryRslZxbsCtrLoanCont, new ZxbsCtrLoanContVO());
            } else {
                logger.debug("当前查询结果为空!");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常!", e);
        }
        return null;
    }

    @Override
    public int insertOrUpdate(List<ZxbsCtrLoanContVO> list) throws Exception {
        return zxbsCtrLoanContDao.insertOrUpdate((List<ZxbsCtrLoanCont>) beansCopy(list, ZxbsCtrLoanCont.class));
    }
}
